﻿#if UNITY_IPHONE
using System.Runtime.InteropServices;
#endif
using UnityEngine;

namespace XUDocking
{
    public class XUDockingCaller
    {
        public static void Init(string json)
        {
            if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
            {
                Standalone.Call("Init", json);
            }
            else if (Application.platform == RuntimePlatform.Android)
            {
                Android.Call("Init", json);
            }
            else if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
                iOS.Init(json);
            }
        }

        public static void Login(string json)
        {
            if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
            {
                Standalone.Call("Login", json);
            }
            else if (Application.platform == RuntimePlatform.Android)
            {
                Android.Call("Login", json);
            }
            else if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
                iOS.Login(json);
            }
        }

        public static void Logout(string json)
        {
            if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
            {
                Standalone.Call("Logout", json);
            }
            else if (Application.platform == RuntimePlatform.Android)
            {
                Android.Call("Logout", json);
            }
            else if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
                iOS.Logout(json);
            }
        }

        public static void Pay(string json)
        {
            if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
            {
                Standalone.Call("Pay", json);
            }
            else if (Application.platform == RuntimePlatform.Android)
            {
                Android.Call("Pay", json);
            }
            else if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
                iOS.Pay(json);
            }
        }

        public static void RoleCreate(string json)
        {
            if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
            {
                Standalone.Call("RoleCreate", json);
            }
            else if (Application.platform == RuntimePlatform.Android)
            {
                Android.Call("RoleCreate", json);
            }
            else if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
                iOS.RoleCreate(json);
            }
        }

        public static void RoleLogin(string json)
        {
            if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
            {
                Standalone.Call("RoleLogin", json);
            }
            else if (Application.platform == RuntimePlatform.Android)
            {
                Android.Call("RoleLogin", json);
            }
            else if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
                iOS.RoleLogin(json);
            }
        }

        public static void RoleLevel(string json)
        {
            if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
            {
                Standalone.Call("RoleLevel", json);
            }
            else if (Application.platform == RuntimePlatform.Android)
            {
                Android.Call("RoleLevel", json);
            }
            else if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
                iOS.RoleLevel(json);
            }
        }

        public static void Exit(string json)
        {
            if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
            {
                Standalone.Call("Exit", json);
            }
            else if (Application.platform == RuntimePlatform.Android)
            {
                Android.Call("Exit", json);
            }
            else if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
                iOS.Exit(json);
            }
        }

        public static void ExtendInterface(string json)
        {
            if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
            {
                Standalone.Call("ExtendInterface", json);
            }
            else if (Application.platform == RuntimePlatform.Android)
            {
                Android.Call("ExtendInterface", json);
            }
            else if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
                iOS.ExtendInterface(json);
            }
        }

        //====================================================================================================

        private class Standalone
        {
            public static void Call(string methodName, object param = null)
            {
#if UNITY_STANDALONE
#endif
            }
        }

        private class Android
        {
            public static void Call(string methodName, object param = null)
            {
#if UNITY_ANDROID
                using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
                {
                    using (AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
                    {
                        if (param == null)
                        {
                            activity.Call(methodName);
                        }
                        else
                        {
                            activity.Call(methodName, param);
                        }
                    }
                }
#endif
            }
        }

        private class iOS
        {
#if UNITY_IPHONE
            [DllImport("__Internal")]
            public static extern void Init(string json);
            [DllImport("__Internal")]
            public static extern void Login(string json);
            [DllImport("__Internal")]
            public static extern void Logout(string json);
            [DllImport("__Internal")]
            public static extern void Pay(string json);
            [DllImport("__Internal")]
            public static extern void RoleCreate(string json);
            [DllImport("__Internal")]
            public static extern void RoleLogin(string json);
            [DllImport("__Internal")]
            public static extern void RoleLevel(string json);
            [DllImport("__Internal")]
            public static extern void Exit(string json);
            [DllImport("__Internal")]
            public static extern void ExtendInterface(string json);
#else
            public static void Init(string json) { }
            public static void Login(string json) { }
            public static void Logout(string json) { }
            public static void Pay(string json) { }
            public static void RoleCreate(string json) { }
            public static void RoleLogin(string json) { }
            public static void RoleLevel(string json) { }
            public static void Exit(string json) { }
            public static void ExtendInterface(string json) { }
#endif
        }
    }
}
